<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\model;
use think\Model;
use think\facade\Db;
class RouteModel{
    const routePath='data/cache/';//路由缓存路径
    //栏目路由
    public function updateCatRoute(){
        //栏目路由
        $catRoute=Db::name("cat")->field("name,catid,classpath")->where("classpath <>''")->select();
        //
        $str='<?php 
use think\facade\Route;
';
        foreach($catRoute as $v){
            //栏目首页
            $str.='Route::get("'.$v['classpath'].'index$","\app\index\controller\InfoList@index")->append(["catid" => '.$v['catid'].'])->ext("html");
';
            $str.='Route::get("'.$v['classpath'].'$","\app\index\controller\InfoList@index")->append(["catid" => '.$v['catid'].']);
';
            //栏目分页
            $str.='Route::get("'.$v['classpath'].'index<page>$","\app\index\controller\InfoList@index")->append(["catid" => '.$v['catid'].'])->ext("html");
';              
            //内容详情
            $str.='Route::get("'.$v['classpath'].'<id>$","\app\index\controller\InfoView@index")->pattern(["id" => "\d+"])->append(["catid" => '.$v['catid'].'])->ext("html");
';        
        }         
        file_put_contents(base_path().self::routePath."catRoute.php", $str);     
    }
    //信息路由
    public function updateInfoRoute(){
        //信息路由
        $table=Db::name("table")->field("table_name")->column("table_name");
        $infoRoute=[];
        foreach($table as $v){
            $rs=Db::name("cms_".$v)->where("titleurl <>''")->field("id,catid,title,titleurl")->select()->toArray(); 
            $infoRoute=array_merge($infoRoute,$rs);
        }
        //
        $str='<?php 
use think\facade\Route;
';   
        foreach($infoRoute as $v){
            $v['titleurl']=str_replace(".html","",$v['titleurl']);
            $str.='Route::get("'.$v['titleurl'].'$","\app\index\controller\InfoView@index")->append(["catid" => '.$v['catid'].',"id" => '.$v['id'].'])->ext("html");
';          
        }            
        file_put_contents(base_path().self::routePath."infoRoute.php", $str);
    }
    //单页路由
    public function updatePageRoute(){
        //单页路由
        $pageRoute=Db::name("page")->field("name,page_id,classpath")->where("classpath <>''")->select(); 
        //
        $str='<?php 
use think\facade\Route;
';         
        foreach($pageRoute as $v){
            $v['classpath']=str_replace(".html","",$v['classpath']);
            $str.='Route::get("'.$v['classpath'].'$","\app\index\controller\Page@index")->append(["page_id" => '.$v['page_id'].'])->ext("html");
';          
        }         
        file_put_contents(base_path().self::routePath."pageRoute.php", $str);
    }    
}
